﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WebGetter
{
    public class RegexHelper
    {
        public string Content { get; private set; }
        public Dictionary<string, List<string>> DicData = new Dictionary<string, List<string>>();

        private List<string> emptyList = new List<string>();

        public RegexHelper(string content)
        {
            Content = content;
        }

        public List<string> this[string keyName]
        {
            get
            {
                return DicData[keyName] ?? emptyList;
            }
        }

        public string GetKeyItem(string key)
        {
            string res = string.Empty;
            if (DicData.ContainsKey(key))
            {
                if (DicData[key] != null && DicData[key].Count > 0)
                {
                    res = DicData[key][0];
                }
            }
            return res;
        }

        public string GetSingleStringByRegexp(string regExp)
        {
            Regex reg = new Regex(regExp);
            var match = reg.Match(Content);
            if (match.Success)
            {
                return match.Groups["val"].Value;
            }
            return string.Empty;
        }

        public void GetAllByRegexp(string regExp)
        {
            Regex reg = new Regex(regExp, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
            var matches = reg.Matches(Content);
            foreach (Match match in matches)
            {
                foreach (var groupName in reg.GetGroupNames())
                {
                    //default group Name should be pass by;
                    if (groupName == "0")
                    {
                        continue;
                    }
                    var list = GetNamedValueList(groupName);
                    list.Add(match.Groups[groupName].Value);
                }
            }
        }

        public void GetByRegexp(string Name, string regExp)
        {
            Regex reg = new Regex(regExp, RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            var match = reg.Matches(Content);
            List<string> list = GetNamedValueList(Name);
            foreach (Match item in match)
            {
                list.Add(item.Groups["val"].Value);
            }
        }

        private List<string> GetNamedValueList(string Name)
        {
            List<string> list;
            if (!DicData.ContainsKey(Name))
            {
                list = new List<string>();
                DicData.Add(Name, list);
            }
            else
            {
                list = DicData[Name];
                //TODO:whether clear it
                //list.Clear();
            }
            return list;
        }
    }
}
